﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestHexGrid : MonoBehaviour
{
    Camera _camera;
    HexGrid _grid;

    public GameObject objectccc;

    private GameObject[,] testObjAry;
    
    // Start is called before the first frame update
    void Start()
    {
        _camera = Camera.main;
        // create template
        var obj = objectccc;
        
        int length = 5;
        int width = 5;

        int centralPoint = 2;

        testObjAry = new GameObject[width, length];

        float adjoinX = 1.4f;
        for (int y = 0; y < width; y++)
        {
            for (int x = 0; x < length; x++)
            {
                //中心点 2/2
                float offX = y % 2 != 0 ? -(adjoinX/2) : 0;
                var c = Instantiate(obj);
                c.transform.localPosition = new Vector3((x - centralPoint) * adjoinX + offX, 0, (y-centralPoint) * 1.2f);
                testObjAry[y, x] = c;
                c.name = string.Format("x:{0}_y:{1}", y,x);
                // itemList[y][x] = obj
            }
        }
        
        
        // int w = 5;
        // int h = 5;
        // _grid = new HexGrid(w, h, 1);
        // _grid.EdgeLength = scale / 2f *1.3f;
        //
        // for (int y = 0; y < h; y++)
        // {
        //     for (int x = 0; x < w; x++)
        //     {
        //         var wpos = _grid.GetWorldPos(x, y);
        //         var c = Instantiate(obj);
        //         c.transform.position = wpos;
        //         c.name = string.Format("{0},{1}", x, y);
        //         _grid.GetCell(x, y).SetObj(0, c);
        //     }
        // }
        //
        // for (int y = 0; y < h; y++)
        // {
        //     for (int x = 0; x < w; x++)
        //     {
        //         var c = _grid.GetCell(x, y).GetObj(0) as GameObject;
        //         var cp = _grid.GetGridPos(c.transform.position.x, c.transform.position.z);
        //         Debug.LogFormat("{0},cpos:{1},{2}", c, cp.x, cp.y);
        //     }
        // }
 
        _inited = true;
    }
 
    public bool IsInRange(int x,int y)
    {
        return x >= 0 && x < 5 && y >= 0 && y < 5;
    }
    
    public Vector2Int GetNear(int x,int y,HexGrid.Dir dir)
    {
        bool up = x % 2 != 1;
        int nx, ny;
        nx = x;ny = y;
        switch (dir)
        {
            case HexGrid.Dir.Top:
                ny += 1;
                break;
            case HexGrid.Dir.RightTop:
                nx += 1;
                if(up)
                    ny += 1;
                break;
            case HexGrid.Dir.RightBottom:
                nx += 1;
                if (!up)
                    ny -= 1;
                break;
            case HexGrid.Dir.Bottom:
                ny -= 1;
                break;
            case HexGrid.Dir.LeftBottom:
                nx -= 1;
                if (!up)
                    ny -= 1;
                break;
            case HexGrid.Dir.LeftTop:
                nx -= 1;
                if (up)
                    ny += 1;
                break;
            default:
                break;
        }
        return new Vector2Int(nx, ny);
    }
    
    
    bool _inited = false;
    GameObject _preSelect = null;
    private void Update()
    {
        if (!_inited) return;
        var ray = _camera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(ray,out hit, 500))
        {
            var c = hit.collider.gameObject;
            if (c == _preSelect)
                return;
            else
            {
                ClearSelect();
                _preSelect = c;
                Select();
            }
        }
        else
        {
            ClearSelect();
        }
    }
 
    private void Select()
    {
        if (_preSelect == null)
            return;
 
        var c = _preSelect;
        SetColor(c, Color.cyan);
        var cpos = _grid.GetGridPos(c.transform.position.x, c.transform.position.z);
        for (int i = 0; i < 6; i++)
        {
            var ncpos = _grid.GetNear(cpos.x, cpos.y, (HexGrid.Dir)i);
            if (_grid.IsInRange(ncpos.x, ncpos.y))
            {
                var ncell = _grid.GetCell(ncpos.x, ncpos.y);
                var obj = ncell.GetObj(0) as GameObject;
                SetColor(obj, Color.yellow);
            }
        }
    }
 
    private void ClearSelect()
    {
        if (_preSelect == null)
            return;
 
        var c = _preSelect;
        SetColor(c, Color.white);
        var cpos = _grid.GetGridPos(c.transform.position.x, c.transform.position.z);
        for (int i = 0; i < 6; i++)
        {
            var ncpos = _grid.GetNear(cpos.x, cpos.y, (HexGrid.Dir)i);
            if (_grid.IsInRange(ncpos.x, ncpos.y))
            {
                var ncell = _grid.GetCell(ncpos.x, ncpos.y);
                var obj = ncell.GetObj(0) as GameObject;
                SetColor(obj, Color.white);
            }
        }
        _preSelect = null;
    }
 
    private void SetColor(GameObject obj,Color color)
    {
        var renders = obj.GetComponentsInChildren<Renderer>();
        foreach (var r in renders)
        {
            r.material.color = color;
        }
    }
}